/*
 * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.ahoo.wow.schema.web

import com.fasterxml.classmate.ResolvedType
import com.github.victools.jsonschema.generator.CustomDefinition
import com.github.victools.jsonschema.generator.CustomDefinitionProviderV2
import com.github.victools.jsonschema.generator.SchemaGenerationContext

object ServerSentEventCustomDefinitionProvider : CustomDefinitionProviderV2 {
    const val SPRING_WEB_SERVER_SENT_EVENT = "org.springframework.http.codec.ServerSentEvent"

    override fun provideCustomSchemaDefinition(
        javaType: ResolvedType,
        context: SchemaGenerationContext
    ): CustomDefinition? {
        if (javaType.erasedType.name != SPRING_WEB_SERVER_SENT_EVENT) {
            return null
        }

        val serverSentEventType = if (javaType.typeParameters.isNotEmpty()) {
            val dataType = javaType.typeParameters[0]
            context.typeContext.resolve(ServerSentEvent::class.java, dataType)
        } else {
            context.typeContext.resolve(ServerSentEvent::class.java)
        }

        val standardDefinition = context.createStandardDefinition(serverSentEventType, this)
        return CustomDefinition(standardDefinition)
    }
}
